package yuan.deng.service.impl.store;

import com.github.pagehelper.PageHelper;


import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.session.SqlSession;
import yuan.deng.domain.store.Company;
import yuan.deng.factory.MapperFactory;
import yuan.deng.mapper.store.CompanyMapper;
import yuan.deng.service.store.CompanyService;
import yuan.deng.utils.TransactionUtil;

import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public void save(Company company) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CompanyMapper companyMapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
            String id = UUID.randomUUID().toString().replace(" ","-");
            //company.setId*
            companyMapper.save(company);
            sqlSession.commit();
        }catch (Exception e){
           sqlSession.rollback();
           e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override
    public void delete(String[] strings) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CompanyMapper companyMapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
            for (int i = 0; i <strings.length; i++) {
                companyMapper.delete( strings[i]);
            }
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override
    public void update(Company company) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CompanyMapper companyMapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
            companyMapper.update(company);
            sqlSession.commit();
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        }finally {
           TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public Company findById(String id) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CompanyMapper companyMapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
            Company company = companyMapper.findById(id);
            sqlSession.commit();
            return company;
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public List<Company> findAll(String page) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MapperFactory.getSqlSession();
            CompanyMapper companyMapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
            int i=1;
            if (StringUtil.isNotEmpty(page)){
                i=Integer.parseInt(page);
            }
            PageHelper.startPage(i,5);
            List<Company> companies = companyMapper.findAll();
            return companies;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }

        return null;
    }

}
